package com.dragon.modules.yuegou.client.wechat_mini.service.impl;

import com.dragon.modules.yuegou.client.wechat_mini.client.LiveClient;
import com.dragon.modules.yuegou.client.wechat_mini.common.BaseResponse;
import com.dragon.modules.yuegou.client.wechat_mini.request.GetGoodsWareHouseRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GetLiveInfoRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GetReplayRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GoodsAddRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GoodsAuditRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GoodsDeleteRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GoodsResetAuditRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.GoodsUpdateRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.RoomAddgoodsRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.RoomCreateRequest;
import com.dragon.modules.yuegou.client.wechat_mini.response.GetGoodsWareHouseResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.GetLiveInfoResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.GetReplayResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.GoodsAddResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.GoodsGetApprovedResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.MediaUploadResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.RoomCreateResponse;
import com.dragon.modules.yuegou.client.wechat_mini.security.AccessTokenService;
import com.dragon.modules.yuegou.client.wechat_mini.service.LiveService;
import com.dragon.modules.yuegou.web.Check;

import java.io.File;
import java.util.function.UnaryOperator;

/**
 * 直播服务实现
 * 
 * @author pengxin
 *
 */
public class LiveServiceImpl implements LiveService {

	/**
	 * 定义直播客户端
	 */
	private final LiveClient client;

	/**
	 * 凭证服务
	 */
	private final AccessTokenService tokenService;

	/**
	 * 构造一个直播服务
	 * 
	 * @param client
	 *            小程序账号配置
	 * @param tokenService
	 *            凭证服务
	 */
	public LiveServiceImpl(LiveClient client, AccessTokenService tokenService) {
		super();
		Check.notNull(client, "client is required");
		Check.notNull(tokenService, "tokenService is required");
		this.client = client;
		this.tokenService = tokenService;
	}

	@Override
	public MediaUploadResponse mediaUpload(String type, File file) {
		return tokenService.executeAutoRetry(accessToken -> client.mediaUpload(accessToken, type, file),
				UnaryOperator.identity());
	}

	@Override
	public MediaUploadResponse mediaUploadImage(File file) {
		return mediaUpload(null, file);
	}

	@Override
	public RoomCreateResponse roomCreate(RoomCreateRequest request) {
		return tokenService.executeAutoRetry(accessToken -> client.roomCreate(accessToken, request),
				UnaryOperator.identity());
	}

	@Override
	public GetLiveInfoResponse getLiveInfo(GetLiveInfoRequest request) {
		return tokenService.executeAutoRetry(accessToken -> client.getLiveInfo(accessToken, request),
				UnaryOperator.identity());
	}

	@Override
	public GetReplayResponse getReplay(GetReplayRequest request) {
		return tokenService.executeAutoRetry(accessToken -> client.getReplay(accessToken, request),
				UnaryOperator.identity());
	}

	@Override
	public void roomAddgoods(RoomAddgoodsRequest request) {
		BaseResponse response = tokenService.executeAutoRetry(accessToken -> client.roomAddgoods(accessToken, request),
				UnaryOperator.identity());
		response.check();
	}

	@Override
	public GoodsAddResponse goodsAdd(GoodsAddRequest request) {
		return tokenService.executeAutoRetry(accessToken -> client.goodsAdd(accessToken, request),
				UnaryOperator.identity());
	}

	@Override
	public void goodsResetAudit(GoodsResetAuditRequest request) {
		BaseResponse response = tokenService.executeAutoRetry(
				accessToken -> client.goodsResetAudit(accessToken, request), UnaryOperator.identity());
		response.check();
	}

	@Override
	public void goodsAudit(GoodsAuditRequest request) {
		BaseResponse response = tokenService.executeAutoRetry(accessToken -> client.goodsAudit(accessToken, request),
				UnaryOperator.identity());
		response.check();
	}

	@Override
	public void goodsDelete(GoodsDeleteRequest request) {
		BaseResponse response = tokenService.executeAutoRetry(accessToken -> client.goodsDelete(accessToken, request),
				UnaryOperator.identity());
		response.check();
	}

	@Override
	public void goodsUpdate(GoodsUpdateRequest request) {
		BaseResponse response = tokenService.executeAutoRetry(accessToken -> client.goodsUpdate(accessToken, request),
				UnaryOperator.identity());
		response.check();
	}

	@Override
	public GetGoodsWareHouseResponse getGoodsWareHouse(GetGoodsWareHouseRequest request) {
		return tokenService.executeAutoRetry(accessToken -> client.getGoodsWareHouse(accessToken, request),
				UnaryOperator.identity());
	}

	@Override
	public GoodsGetApprovedResponse goodsGetApproved(Integer offset, Integer limit, Integer status) {
		return tokenService.executeAutoRetry(accessToken -> client.goodsGetApproved(accessToken, offset, limit, status),
				UnaryOperator.identity());
	}

}
